package com.cn.yunhe.rentcars.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.captcah.BusiLogUtil;
import com.cn.yunhe.rentcars.mapper.BusiLogMapper;
import com.cn.yunhe.rentcars.mapper.CreditMapper;
import com.cn.yunhe.rentcars.model.ClientUser;
import com.cn.yunhe.rentcars.model.Credit;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 信用记录管理
 * @author 刘建国
 *
 */

@RestController
public class CreditController {
	@Autowired
	private CreditMapper creditMapper;
	@Autowired
    private BusiLogMapper busiLogMapper;
	
	/**
	 * 跳转到信用记录管理页面
	 * @return 页面
	 *
	 */
	@RequestMapping(value="/credits", method=RequestMethod.GET)
	public ModelAndView showPageCredit(){
		
		return new ModelAndView("credit-list");
	}
	
	/**
	 * 跳到信用记录页面
	 * @return 页面
	 *
	 */
	
	@RequestMapping(value="/credit-add", method=RequestMethod.GET)
	public ModelAndView PageCredit(){
		
		return new ModelAndView("credit-add");
	}
	
	
	/**
	 * 模糊查询客户信用列表（支持分页）
	 * @param 
	 * @return
	 *
	 * @param draw 		[jquery dattable特有的固有的参数，第几次点击查询]
	 * * @param start	[jquery dattable特有的固有的参数，从第几条开始查询]
	 * @param length	[jquery dattable特有的固有的参数，每页都少条数据]
	 * @return
		 */
		@RequestMapping(value="/creditsGetlikecond", method=RequestMethod.GET)
		public Map<String,Object> getClientCond(@RequestParam String idcard,@RequestParam String mobile,@RequestParam int draw,@RequestParam int start,@RequestParam int length){
			System.out.println("cvv");
			//TODO:把输入参数封装费Map
			Map<String,String> paramsMap = new HashMap<String,String>();
			paramsMap.put("idcard", idcard);
			paramsMap.put("mobile", mobile);
			
			int pageNum = (start/length)+1;	//获取当前查询第几页
			int pageSize = length;
			//TODO:调用DAO MAPPER数据层传入MAP参数去模糊查询，返回List结构
			PageHelper.startPage(pageNum,pageSize);
			List<Credit> credit = creditMapper.searchCredits(paramsMap);
			//System.out.println(credit.size());
			PageInfo<Credit> page = new PageInfo<Credit>(credit);
			
			Map<String,Object> rsMap = new HashMap<String,Object>();
			rsMap.put("draw",draw);
		    rsMap.put("data", page.getList());	      
		    rsMap.put("recordsTotal", page.getTotal());
		    rsMap.put("recordsFiltered", page.getTotal());
			return rsMap;
		}
		
		
		/*
		 * 
		 * 添加客户信用记录
		 */
		@RequestMapping(value="/addCredit",method=RequestMethod.POST)
		public void addCredit(@RequestBody Credit credit,HttpServletRequest request){
			credit.setCid(UUID.randomUUID().toString());
			creditMapper.addCredit(credit);
			//添加日志信息
            Map<String,Object> busMap = new HashMap<String,Object>();
            busMap.put("logid", UUID.randomUUID().toString());
            busMap.put("businame", "添加客户信用记录");
            busMap.put("busitype", "添加");
            busMap.put("busiconent","添加客户信用记录");
            BusiLogUtil.addBusiLog(request, busiLogMapper, busMap);
		}
		
}
